<template>
    <el-form :model="memberInfo" status-icon :rules="rules" ref="memberInfo" label-width="100px" class="demo-ruleForm">
        <h2>添加会员</h2>
        <el-form-item label="账号" prop="accountName">
            <el-input type="text" v-model="memberInfo.accountName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开通时间" prop="openTime">
            <el-date-picker
                v-model="memberInfo.openTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd hh:mm:ss"
                default-time="12:00:00">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间" prop="endTime">
            <el-date-picker
                v-model="memberInfo.endTime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd hh:mm:ss"
                default-time="12:00:00">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="年限" prop="openCount">
            <el-select v-model="memberInfo.openCount" placeholder="请选择年限">
                <el-option label="一年" value="1"></el-option>
                <el-option label="两年" value="2"></el-option>
                <el-option label="三年" value="3"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="子账号(选填)" prop="subAccountName">
            <el-col :span="6">
                <el-input type="text" v-model="memberInfo.subAccountName[0]" autocomplete="off"></el-input>
            </el-col>
            <el-col :span="1" align="right">&emsp;</el-col> 
            <el-col :span="6">
                <el-input type="text" v-model="memberInfo.subAccountName[1]" autocomplete="off"></el-input>
            </el-col> 
            <el-col :span="1" align="right">&emsp;</el-col>
            <el-col :span="6">
                <el-input type="text" v-model="memberInfo.subAccountName[2]" autocomplete="off"></el-input>
            </el-col> 
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
            <el-input type="text" v-model="memberInfo.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
            <el-input type="text" v-model="memberInfo.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('memberInfo')">提交</el-button>
            <el-button @click="resetForm('memberInfo')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import {mapMutations} from 'vuex'
 export default {
    data() {
      return {
        memberInfo: {
            accountName: '',
            openTime: '',
            endTime:'',
            openCount:'',
            subAccountName:['','',''],
            userName:'',
            address:''
        },
        rules:{
            accountName:[{ required: true, message: '请输入账号', trigger: 'blur' }],
            openTime:[{required:true, message:'请选择日期', trigger: 'blur'}],
            endTime:[{required:true, message:'请选择日期', trigger: 'blur'}],
            openCount:[{ required: true, message: '请选择年限', trigger: 'blur' }],
            userName:[{ required: true, message: '输入姓名', trigger: 'blur' }],
            address:[{ required: true, message: '输入业主地址', trigger: 'blur' }],
        }
      };
    },
    methods: {
        ...mapMutations({
            addMember:'member/addMember'
        }),
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                const loading = this.$loading({
                    lock: true,
                    text: '添加成功',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                this.addMember(this.memberInfo);
                setTimeout(() => {
                    loading.close();
                    this.$router.push({path:'/member'})
                }, 1000);
                
            } else {
                return false;
            }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
  }
</script>

<style>

</style>
